package cn.maydaymemory.mastery.listener;

import cn.maydaymemory.mastery.shop.SellInventory;
import cn.maydaymemory.mastery.shop.Shop;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;

public class ShopInventoryListener implements Listener {
    @EventHandler
    public void onInventoryClose(InventoryCloseEvent event){
        if(SellInventory.isViewer(event.getPlayer().getName())){
            SellInventory.stopView(event.getPlayer().getName());
            if(!SellInventory.isViewer(event.getPlayer().getName())){
                SellInventory sl = SellInventory.getInstance(event.getPlayer().getName());
                if(sl == null) return;
                sl.settle();
            }
        }
        Shop.stopGoodsListEdit(event.getPlayer());
        Shop.stopGoodsListView(event.getPlayer());
    }

    @EventHandler
    public void onSellInventoryView(InventoryClickEvent event){
        Inventory i = event.getClickedInventory();
        if(i!=null){
            HumanEntity player = event.getWhoClicked();
            if(SellInventory.isViewer(player.getName())){
                SellInventory sl = SellInventory.getInstance(player.getName());
                if(sl == null) return;
                event.setCancelled(true);
                if(event.getClickedInventory().getHolder()==null) {
                    sl.removeItem(event.getSlot());
                }
                if(event.getClickedInventory().getHolder() instanceof HumanEntity){
                    sl.addItem(event.getSlot());
                }
            }
        }
    }

    @EventHandler
    public void onGoodsListInventoryView(InventoryClickEvent event){
        Inventory i = event.getClickedInventory();
        if(i!=null){
            if(Shop.isGoodsListViewer(event.getWhoClicked())) {
                event.setCancelled(true);
                if(event.getClickedInventory().getHolder()==null) {
                    int indexInv  = Shop.getInventoryIndex(event.getClickedInventory());
                    if(event.getSlot() == 27){
                        if(event.getCurrentItem()!=null&&!event.getCurrentItem().getType().equals(Material.AIR)) {
                            event.getWhoClicked().closeInventory();
                            Shop.viewGoodsList(event.getWhoClicked(),indexInv-1);
                        }
                    }
                    if(event.getSlot() == 35){
                        if(event.getCurrentItem()!=null&&!event.getCurrentItem().getType().equals(Material.AIR)) {
                            event.getWhoClicked().closeInventory();
                            Shop.viewGoodsList(event.getWhoClicked(),indexInv+1);
                        }
                    }
                }
            }
        }
    }

    @EventHandler
    public void onGoodsListInventoryEdit(InventoryClickEvent event){
        Inventory i = event.getClickedInventory();
        if(i!=null){
            if(Shop.isGoodsListEditor(event.getWhoClicked())){
                event.setCancelled(true);
                if(event.getClickedInventory()!=null && event.getClickedInventory().getHolder()==null) {
                    int indexInv  = Shop.getInventoryIndex(event.getClickedInventory());
                    int index = indexInv*27+event.getSlot();
                    if(event.getSlot() == 27){
                        if(event.getCurrentItem()!=null&&!event.getCurrentItem().getType().equals(Material.AIR)) {
                            event.getWhoClicked().closeInventory();
                            Shop.editGoodsList(event.getWhoClicked(),indexInv-1);
                            return;
                        }
                    }
                    if(event.getSlot() == 35){
                        if(event.getCurrentItem()!=null&&!event.getCurrentItem().getType().equals(Material.AIR)) {
                            event.getWhoClicked().closeInventory();
                            Shop.editGoodsList(event.getWhoClicked(),indexInv+1);
                            return;
                        }
                    }
                    if(index>=0 && index<Shop.getGoodsList().size()){
                        Shop.deleteGoods(index);
                        event.getWhoClicked().closeInventory();
                        Shop.editGoodsList(event.getWhoClicked(),indexInv);
                    }
                }
            }
        }
    }
}
